<!DOCTYPE html>
<html>

<head>
    <title>表单</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>

<body>
    <h1>注册表单</h1>
    <form action="#" method="post">
        <p>
            <label>用&nbsp;户&nbsp;名:</label>
            <input type="text" id="username" />
        </p>
        <p>
            <label>学&nbsp;&nbsp;&nbsp;号:</label>
            <input type="text" id="id" />
        </p>
        <p>
            <label>年&nbsp;&nbsp;&nbsp;龄:</label>
            <input type="text" id="age" />
        </p>
        <!-- <p>
            <input type="submit" id="submit" name="submit" value="提交" />
            <input type="reset" name="reset" value="重置" />
            <input type="button" id="getFromServer" onclick="getFromServerfunc()" name="" value="获取服务器信息" />
        </p> -->
        <p>
            <input type="button" id="test" onclick="testget()" value="testget" />
            <input type="button" id="test" onclick="testpost()" value="testpost" />
        </p>
    </form>
    <script type="text/javascript">
        // console.log("haha")
        
        function testget() {
            $.ajax({
                url: 'http://localhost:8080/user/getUser',
                method: 'GET'
            }).done(function (result) {
                console.log(result);
                $("#username").attr("value",result['username']);
                $("#id").attr("value",result['id']);
                $("#age").attr("value",result['age']);
            }).fail(function (jqXHR, textStatus) {
                console.log(textStatus);
            }).complete(function (jqXHR, data) {
                console.log("获取header:");
                var list = jqXHR.getAllResponseHeaders();
                console.log(list);
            });
        }
        function testpost() {
            var postdata={
                "id":$("#id").val(),
                "username":$("#username").val(),
                "age":$("#age").val()
            }
            $.ajax({
                url: 'http://localhost:8080/user/postUser',
                method: 'POST',
                contentType: 'application/json',
                // dataType:"text",
                data: JSON.stringify(postdata)
            }).done(function (result) {
                console.log("done" + result);

            }).fail(function (jqXHR, textStatus) {
                console.log("fail" + "---" + textStatus);
            }).complete(function (jqXHR, data) {
                console.log("获取header:");
                var list = jqXHR.getAllResponseHeaders();
                console.log(list);
            });
        }
        function getFromServerfunc() {
            
            console.log(JSON.stringify(postdata));
        }
    </script>
</body>

</html>